package com.chanyachting.emall.action.member;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Companymember;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.Memberopertionmenu;
import com.chanyachting.emall.domain.Menugroup;
import com.chanyachting.emall.exception.BuisnessException;
import com.chanyachting.emall.form.MemberForm;
import com.chanyachting.emall.service.CompanyMemberService;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.service.MemberopertionmenuBO;
import com.chanyachting.emall.serviceImpl.CompanyMemberServiceImpl;
import com.chanyachting.emall.serviceImpl.MemberServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		MemberForm mform=(MemberForm)form;
		
		Member member=new Member();
		
		member.setAccount(mform.getAccount());
		member.setPassword(mform.getPassword());
		
//		member.setMemberType(mform.getMemberType());
		member.setLoginDate(DateUtil.getDate());
		member.setLoginIP(request.getRemoteHost());
		
		
		MemberService memberService=new MemberServiceImpl();
		
		try {
			
			member=memberService.login(member);
			
			//会员登陆成功，初始化会员后台功能菜单
			List<Memberopertionmenu> memberMenuList=Memberopertionmenu.listByMemberId(member.getId());
			
			
			
			//将会员菜单列表，转换成标准的 菜单组-- 菜单列表 格式
			List<Menugroup> menuGroupList=MemberopertionmenuBO.getMenugroupListByMemberMenu(memberMenuList);
			
		
			
			//保存菜单组列表
			ru.setSessionObject("menuGroupList", menuGroupList);
			
			//保存会员对象到session
			ru.setSessionObject(MemberService.MEMBER_IN_SESSION,member);
			
			if(member.isCompanyMember()){
				
				//如果是公司会员，同时获得会员对应的公司对象，并保存到session中
				Company company=new Company();

				//根据会员对象，获得公司会员对象
				Companymember cmember=memberService.getCompanyMemberByMember(member);
				
				if(cmember!=null){
					
					//根据公司会员对象，获得公司对象 
					CompanyMemberService cms=new CompanyMemberServiceImpl();
					
					company=cms.getCompanyByCompanymember(cmember);
					
					//保存公司对象到session
					ru.setSessionObject(CompanyService.COMPANY_IN_SESSION, company);
				}
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
			request.setAttribute(BuisnessException.MSG_IN_REQUEST, e.getMessage());
			
			return mapping.findForward("login");
		}
		
		return mapping.findForward("loginSuccess");
	}
	
	

}
